#pragma once

#if 0
#include "game.h"
#else
#include "game2.h"
#endif


#include "ttf.h"
#include <deque>

//move graphics down?
//or move up from game.cpp
#include "game_graphics.h"
#include "game_code.h"
#include "counter.h"
#include "input.h"

//disable counter
/*#define counter_end() 
#define counter_start()
#define counter_update()
#define counter_print()*/

int delay = 0;
int counter = 0;
deque<float> fps_log;
deque<int> elapsed_tick_log;

int main(int argc, char *args[])
{
	ttf_init();
	SDL_Init(SDL_INIT_EVERYTHING);

	Game2 * game = game_get();

	game_setup(game);

	graphics_init(WINDOW_GAME);
	graphics_init(WINDOW_DEBUG);
	debug_graphics_load_number_textures();

	GamePartitionMethods methods = {};
	methods.active_callback = glue_get_global_ents;
	methods.region_callback = glue_get_ents_in_region;
	methods.simulated_callback = glue_get_simulated_ents;
	methods.update_sim_list_from_viewport = glue_update_sim_list;

	GameGraphicMethods draw_methods = {};
	draw_methods.AddRectFillSS = debug_graphics_add_rect_to_stack_ss;
	draw_methods.Begin = debug_graphics_start_render;
	draw_methods.End = debug_graphics_final_render;
	draw_methods.Render = debug_graphics_render_stack;


	BOOL_QUIT is_quitting = false;
	int frame_time_in_ms = 33;
	float av_fps = 0.0f;
	UserInputData input_state = {};

	int slow_counter = 0;

	do
	{
		counter_start(game->_counter, COUNT_ID::C_total_loop);
		
		int before = SDL_GetTicks();

		//every 50 ms
		bool print_time = false;
		if (before > counter){
			print_time = true;
			counter += 50;
			slow_counter++;
		}

		//RUN GAME
		//Input__GetSpace();
		counter_start(game->_counter,COUNT_ID::C_input);
		SDL_Event event;
		input_state = Input__GetInputState(&event,input_state);

		if (Input__IsKey(input_state,KeyMask::KEY_UP,KeyState::KEY_STATE_PRESSED))
		{
			printf("up\n");
		}
		if(Input__IsKey(input_state,KeyMask::KEY_DOWN,KeyState::KEY_STATE_PRESSED))
		{
			printf("down\n");
			is_quitting = true;

			delay = 0;
		}
		if(Input__IsKey(input_state,KeyMask::KEY_LEFT,KeyState::KEY_STATE_PRESSED))
		{
			printf("left\n");
			
			is_quitting = true;

			delay += 5;
			
		}
		counter_end(game->_counter,COUNT_ID::C_input);
				counter_start(game->_counter,COUNT_ID::C_graphics_text);
		if (print_time && slow_counter %10 ==0)
		{

		debug_graphics_update_debug_window();
		
		}
		counter_end(game->_counter,COUNT_ID::C_graphics_text);

		
		counter_start(game->_counter, COUNT_ID::C_main);

		{	//simulate load
				SDL_Delay(delay);
		}
		if( game_run(game,frame_time_in_ms/1000.0f,methods, draw_methods)){is_quitting = true;}
		

		int after = SDL_GetTicks();
		counter_end(game->_counter, COUNT_ID::C_main);
		counter_start(game->_counter,COUNT_ID::C_loop_other);

		frame_time_in_ms = (after - before);
		if (frame_time_in_ms == 0)				//if SDL thinks no time has passed since last frame...
		{
			SDL_Delay(1);
			frame_time_in_ms++;
		}
		float fps = 1000.0f / frame_time_in_ms;
		fps_log.push_back(fps);
		
		if (fps_log.size() > 200){fps_log.pop_front();}

		elapsed_tick_log.push_back(after);
		
		if (elapsed_tick_log.size() > 50)
		{
			av_fps= 50.0f * 1000.0 / (float)(after - elapsed_tick_log.front());
			elapsed_tick_log.pop_front();
		}

		//average fps and print
		float average = 0;
		if (print_time)
		{
			deque<float>::iterator iter;
			float sum = 0;
			int num = 0;
			for (iter = fps_log.begin(); iter != fps_log.end(); iter++)
			{
				sum += *iter;
				num++;
			}
			float average = sum / (float) num;
			 printf("navfps = %f, av fps = %f, fps = %3.3f, ms = %d \n",(float)av_fps, average , fps, frame_time_in_ms);
			debug_graphics_set_number(2,average);
		}



		counter_end(game->_counter,COUNT_ID::C_loop_other);
		counter_end(game->_counter, COUNT_ID::C_total_loop);
		if (print_time){
			/*
			char buff1[100];
			char buff2[100];
			char buff3[100];*/
			double str1 = counter_get_contribution(game->_counter,COUNT_ID::C_graphics_text);
			double str2 = counter_get_contribution(game->_counter,COUNT_ID::C_main);
			/*sprintf(buff1, "text %f",100.0f*str1);
			sprintf(buff2, "main %f",100.0f*str2);
			sprintf(buff3, "navfps = %f, av fps = %f, fps = %3.3f, ms = %d \n",(float)av_fps, average , fps, frame_time_in_ms);
			*/
			debug_graphics_set_text(0,"text % ");
			debug_graphics_set_text(1,"main % ");
			debug_graphics_set_text(2,"av fps ");
			debug_graphics_set_number(0,str1);
			debug_graphics_set_number(1,str2);
			
			//debug_graphics_draw_number(v2i(100,100),123.456);
			counter_print(game->_counter);
		}
		counter_update(game->_counter);


	}
	while (!is_quitting);
	graphics_destroy(WINDOW_GAME);
	graphics_destroy(WINDOW_DEBUG);
	game_destroy(game);
	//game_end();
	ttf_close();
	atexit(SDL_Quit);
	main(argc,args);


	return 0;
}

